package com.taobao.machinesystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taobao.machinesystem.common.Result;
import com.taobao.machinesystem.common.SystemConst;
import com.taobao.machinesystem.entity.SysPermission;
import com.taobao.machinesystem.entity.SysRolePermission;
import com.taobao.machinesystem.entity.vo.SysPermissionVo;
import com.taobao.machinesystem.service.SysPermissionService;
import com.taobao.machinesystem.service.SysRolePermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author LiuJunCheng
 * @since 2022-04-22
 */
@Slf4j
@RestController
@RequestMapping("/machine_system/sys-permission")
public class SysPermissionController {

    @Autowired
    private SysPermissionService sysPermissionService;
    @Autowired
    private SysRolePermissionService sysRolePermissionService;


    @GetMapping("/list/{currentPage}/{size}/")
    public Result list(
            @PathVariable("currentPage") Integer currentPage,
            @PathVariable("size") Integer size) {

        Page<SysPermission> page = new Page<>(currentPage, size);

        sysPermissionService.page(page);

        return Result.ok()
                .setMessage(SystemConst.SELECT_SUCCESS)
                .setMapValue("total", page.getTotal())
                .setMapValue("items", page.getRecords());
    }
    /**
     * 根据角色id查询对应的权限
     * @param id
     * @return
     */
    @PostMapping("/selectRolePermission/{id}")
    public Result selectRolePermission(@PathVariable("id") String id){
        //获取到角色的权限
        List<SysPermissionVo> sysPermissionVos = this.sysPermissionService.selectRolePermission(id);
        //获取到所有权限
        List<SysPermissionVo> sysPermissionVoss = this.sysPermissionService.selectRolePermission("1");
        return Result.ok()
                .setMessage(SystemConst.SELECT_SUCCESS)
                .setMapValue("items", sysPermissionVos)
                .setMapValue("sumItems", sysPermissionVoss);
    }

    @PostMapping("/create")
    public Result save(@RequestBody SysPermission sysPermission) {
        if (Objects.isNull(sysPermission)) {
            return Result.error().setMessage(SystemConst.NOT_NULL_PARAMETER);
        }
        boolean save = sysPermissionService.save(sysPermission);

        if (save) {
            return Result.ok().setMessage(SystemConst.INSERT_SUCCESS);
        } else {
            return Result.error().setMessage(SystemConst.INSERT_FAIL);
        }
    }

    /**
     * 更改信息
     *
     * @param sysPermission
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody SysPermission sysPermission) {
        if (!Objects.isNull(sysPermission)) {
            return Result.error().setMessage(SystemConst.NOT_NULL_PARAMETER);
        }

        boolean flag = sysPermissionService.updateById(sysPermission);

        if (flag) {
            log.info("update plan0 success {}", sysPermission.toString());
            return Result.ok().setMessage(SystemConst.UPDATE_SUCCESS);
        } else {
            log.error("update plan0 fail {}", sysPermission.toString());
            return Result.error().setMessage(SystemConst.UPDATE_FAIL);
        }
    }

    /**
     * 删除设备类型
     * @param id
     * @return Result
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        boolean flag = sysPermissionService.removeById(id);
        if (flag) {
            log.info("update plan success {}", id);
            return Result.ok().setMessage(SystemConst.DELETE_SUCCESS);
        } else {
            log.error("update plan fail {}", id);
            return Result.error().setMessage(SystemConst.DELETE_FAIL);
        }
    }
    /**
     * 赋予权限
     * @param roleId
     * @return Result
     */
    @PostMapping("/addRolePermission/{roleId}")
    public Result addRolePermission(@PathVariable String roleId,@RequestParam("ids") String ids){
        System.out.println(roleId);
        System.out.println(ids);
        this.sysRolePermissionService.addRolePermission(ids,roleId);
        return new Result().ok().setMessage(SystemConst.INSERT_FAIL);
    }


}

